﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Blog.Models;
using System.ComponentModel.Composition;

namespace Blog.Controllers
{
    [Export(typeof(IController)), ExportMetadata("Name", "Post")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class PostController : Controller
    {
        public ActionResult Index()
        {
            return View(Context.Instance.GetPosts());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Post postToCreate)
        {
            Context.Instance.CreatePost(postToCreate);
            return RedirectToAction("Index");
        }

        public ActionResult View(Guid id)
        {
            return View(Context.Instance.GetPost(id));
        }

        public ActionResult Edit(Guid id)
        {
            return View(Context.Instance.GetPost(id));
        }

        [HttpPost]
        public ActionResult Edit(Guid id, Post newPost)
        {
            Context.Instance.UpdatePost(id, newPost);
            return RedirectToAction("Index");
        }

        public ActionResult Delete(Guid id)
        {
            return View(Context.Instance.GetPost(id));
        }

        [HttpPost]
        public ActionResult Delete(Guid id, Post deletePost)
        {
            Context.Instance.DeletePost(id);
            return RedirectToAction("Index");
        }
    }
}
